use std::{env,fs::{File, OpenOptions, create_dir_all}, io::{Read, Write, Result}, panic, path::Path};

use clap::{App, Arg};

fn main() {
    let matches = App::new("My Super Program")
    .version("1.0")
    .author("yuzf <yuzf1997@gmail.com>")
    .about("A tool which can translate sass to css")
    .arg(Arg::with_name("INPUT")
         .help("Sets the input file to use")
         .required(true)
         .index(1))
    .arg(Arg::with_name("OUTPUT")
         .help("Sets the output file to use")
         .required(true)
         .index(2))
    .arg(Arg::with_name("watch"))
    .get_matches();
    let current_path = env::current_dir().unwrap();
    let input = matches.value_of("INPUT").unwrap();
    let output = matches.value_of("OUTPUT").unwrap();
    let input_path = current_path.clone().join(input);
    let output_path = current_path.clone().join(output);
    if !input_path.exists() {
        panic!("{} 文件不存在！", input);

    }
    translate(&input_path, &output_path).unwrap();


}


fn translate(input_path:&Path, output_path:&Path)->Result<()>{
     let mut  input_file = File::open(input_path)?;
     let mut content = String::new();
     input_file.read_to_string(&mut content)?;    
     // println!("{}", content);
     let output_parent_path = output_path.parent().unwrap();
     if !output_parent_path.exists(){
          create_dir_all(output_parent_path).expect(&format!("{} 创建失败！", output_path.to_str().unwrap()));
     }
     let mut output_file = OpenOptions::new().read(true).write(true).create(true).open(output_path).expect(&format!("{} 创建失败！", output_path.to_str().unwrap()));
     output_file.write_all(content.as_bytes())?;
     output_file.flush()?;
     Ok(())


}
